package com.intct.hbase.ddl.impl;

import com.intct.hbase.ddl.AbsDDLSerivce;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
import org.apache.hadoop.hbase.client.TableDescriptor;
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;

import java.io.IOException;

/**
 * @author gufg
 * @since 2025-09-29 14:24
 */
public class AlterTable extends AbsDDLSerivce {
    private String namespace;
    private String tName;

    public AlterTable(String namespace, String tName) {
        this.namespace = namespace;
        this.tName = tName;
    }
    @Override
    public void execCommand(Admin admin) {
        // TableName 对象
        TableName tableName = TableName.valueOf(namespace, tName);

        try {
            // 判断存在，则修改
            if (admin.tableExists(tableName)) {
                // 旧表描述
                TableDescriptor oldTableDescriptor = admin.getDescriptor(tableName);

                // 创建表描述 并且与旧表描述关联
                TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(oldTableDescriptor);

                // 创建列族描述
                ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder("f1".getBytes());
                //设置版本
                columnFamilyDescriptorBuilder.setMaxVersions(5);

                // 增加列族
                // tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());

                // 修改列族
                // tableDescriptorBuilder.modifyColumnFamily(columnFamilyDescriptorBuilder.build());

                // 删除列族
                tableDescriptorBuilder.removeColumnFamily("f3".getBytes());

                // 修改表
                admin.modifyTable(tableDescriptorBuilder.build());
            }

        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    public static void main(String[] args) throws IOException {
        new AlterTable("intct85", "t2").start();
    }
}
